/**
 * 
 */
package no.hig.ekm.ircclient;

import java.beans.PropertyVetoException;
import java.util.ResourceBundle;

import javax.swing.JInternalFrame;
import javax.swing.text.SimpleAttributeSet;


/**
 * This class are handling fonts for different text types in the irc client.
 * 	Store them integer the register, ready to use for next time the program starts.
 * 
 * @author morten
 */
public class Fonts extends JInternalFrame {
	//Creating variables
	private static ResourceBundle messages;
	private static SimpleAttributeSet[] sas;
	public static final String[] fontNames = {"default", "server", "private", 
			"channel", "chatStatus", "myText", "action"};
	public static String[] titleNames;
	//Constants used for id in knowing which font belongs to which attribute set
	private static final int noFonts = 7;
	public static final int DEFAULT = 0;
	public static final int SERVER = 1;
	public static final int PRIVATE = 2;
	public static final int CHANNEL = 3;
	public static final int CHATSTATUS = 4;
	public static final int MYOWNTEXT = 5;
	public static final int ACTIONS = 6;
	
	/**
	 * Function that makes an array for storing different
	 *  font selectors with different style attributes for
	 *  different texts in the chat.
	 */
	public static void init() {
		Main.fontSelectors = new FontSelector[noFonts];
		sas = new SimpleAttributeSet[noFonts];
		//Creating an attribute set for each font selector
		for(int i = 0; i < noFonts;  i++) {
			sas[i] = new SimpleAttributeSet();
			Main.fontSelectors[i] = new FontSelector(i);
		}
	}
	
	/**
	 * Function for making the right window focused. And
	 *  edit the right font settings for the right text.
	 * 
	 * @param id - is the id of the fontSelector-window
	 */
	public static void editFont(int id) {
		int j = 0;
		//Getting all the internal frames that is up and running
		JInternalFrame[] frames = Main.win.md.getAllFrames();
		//A for-loop for remove duplicated code
		//The loop traverse through the windows that is up and running in Main.java.
		// Sets j to be 1 if window is found in Main.java
		for(int i = 0; i < frames.length; i++) {
			if(frames[i] == Main.fontSelectors[id]) {
				j = 1;
				break;
			}
		}
		//If the j from the loop is 0 it creates a new fontSelector window
		if(j == 0)
			Main.win.md.add(Main.fontSelectors[id]);
		//If it break from the loop the j is 1 and the window that was meant to be made
		// is just put in front and sets it to be the selected window.
		Main.fontSelectors[id].toFront();
		Main.fontSelectors[id].setVisible(true);
		try {
			Main.fontSelectors[id].setSelected(true);
		} catch (PropertyVetoException e) {
		}
	}
	
	/**
	 * This function gets the id of an fontSelector. Use the id in a
	 *  SimpleAttributeSet-array called sas to get the right attribute set
	 *  for the right fontSelector.
	 * 
	 * @param id - is the id of the fontSelector-window
	 * @return - SimpleAttributeSet sas[id]
	 */
	public static SimpleAttributeSet getFont(int id) {
		return sas[id];
	}
	
	/**
	 * Get id of fontSelector-window and the active attributes for that 
	 *  fontSelector-window. Saves the attribute set in the 
	 *  SimpleAttributeSet-array in tray id with attribute set saas
	 * 
	 * @param id - is the id of the fontSelector-window
	 * @param saas - SimpleAttributeSet saas
	 */
	public static void setFont(int id, SimpleAttributeSet saas) {
		sas[id] = saas;
	}
	
	/**
	 * Used for internationalization 
	 * @param m
	 */
	public static void setMessages(ResourceBundle m) {
		Fonts.messages = m;
		//Creating a array with internationalized fontNames
		titleNames = new String[] {messages.getString("fontsTitleDefault"), 
				messages.getString("fontsTitleServer"), messages.getString("fontsTitlePrivate"),
				messages.getString("fontsTitleChannel"), messages.getString("fontsTitleChatstatus"), 
				messages.getString("fontsTitleMytext"), messages.getString("fontsTitleAction")};
	}
}

